package com.hua.base;

import java.util.HashSet;

public class EqualExample {
    private int x;
    private int y;
    private int z;
    public EqualExample(int x,int y,int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
    @Override
    public boolean equals(Object o){
        if (this == o) return true;   // 检查是否为同一个对象的引用，如果是直接返回 true；
        if (o == null || getClass() != o.getClass()) return false; //检查是否是同一个类型，如果不是，直接返回 false；
        EqualExample that = (EqualExample) o; // 将 Object 对象进行转型；
        if (x != that.x) return false; //判断每个关键域是否相等。
        if (y != that.y) return false;
        return z == that.z;
    }

    public static void main(String[] args) {
        EqualExample ex1 = new  EqualExample(1,1,1);
        EqualExample ex2 = new  EqualExample(1,1,1);
        System.out.println(ex1.equals(ex2));
        HashSet<EqualExample> set =new HashSet<>();
        set.add(ex1);
        set.add(ex2);
        System.out.println(set.size());

    }

    @Override
    public int hashCode(){
        int result =17;
        result = 31 * result + y;
        result = 31 * result + z;
        result = 31 * result + x;
        return result;
    }
}
